package com.dc.business.datacollectbusiness.service.impl;

import com.dc.business.datacollectbusiness.thread.HJ212ReceiverThread;
import com.dc.business.datacollectbusiness.mapper.HJ212ReceiverMapper;
import com.dc.business.datacollectbusiness.model.HJ212Model;
import com.dc.business.datacollectbusiness.model.device;
import com.dc.business.datacollectbusiness.service.HJ212ReceiverService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import lombok.extern.slf4j.Slf4j;
import org.apache.poi.ss.formula.functions.T;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationContext;
import org.springframework.stereotype.Service;

import java.io.IOException;
import java.time.Duration;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.ArrayList;
import java.util.List;
import java.util.Objects;
import java.util.concurrent.Executors;
import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.TimeUnit;

@Service
@Slf4j
public class HJ212ReceiverServiceImpl implements HJ212ReceiverService {

        @Autowired
        private ApplicationContext applicationContext;

        @Autowired
        private HJ212ReceiverMapper hj212ReceiverMapper;

        @Override
        public void collectHJ212Msg() throws IOException {
            for(int i = 10001;i <= 10034 ;i ++){
                HJ212ReceiverMapper bean = applicationContext.getBean(HJ212ReceiverMapper.class);
                HJ212ReceiverThread TaskThread = new HJ212ReceiverThread(String.valueOf(i), bean);
                TaskThread.start();
            }
        }


    @Override
    public List<device> fetchAllDevicesLatestInfo() {
        List<HJ212Model> allDevicesLatestInfo = hj212ReceiverMapper.fetchAllDevicesLatestInfo();
        List<device> deviceList = new ArrayList<>();
        for (HJ212Model devicesLatestInfo : allDevicesLatestInfo) {
            // 根据mn得到设备名称
            device device = setOneDeviceState(devicesLatestInfo);
            deviceList.add(device);
        }
        return deviceList;
    }

    @Override
    public PageInfo<HJ212Model> findPageDeviceByMn(int pageNum, int pageSize, String mn) {
        // 开启分页
        PageHelper.startPage(pageNum, pageSize);
        LocalDate currentDate = LocalDate.now();
        DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd");
        String formattedDate = currentDate.format(formatter);
        List<HJ212Model> hj212Models = hj212ReceiverMapper.findPageDeviceByMn(mn, formattedDate);
        return new PageInfo<>(hj212Models);
    }

    @Override
    public PageInfo<HJ212Model> findPageDeviceByMnAndTime(int pageNum, int pageSize, String mn, String startTime, String endTime) {
        PageHelper.startPage(pageNum, pageSize);
        if (Objects.equals(startTime, endTime)) {
            startTime = startTime + " 00:00:00";
            endTime = endTime + " 23:59:59";
        }
        List<HJ212Model> hj212Models = hj212ReceiverMapper.findPageDeviceByMnAndTime(mn, startTime, endTime);
        return new PageInfo<>(hj212Models);
    }

    public device setOneDeviceState(HJ212Model hj212Model) {
        String deviceMN = hj212Model.getMn();
        double voltageA = Double.parseDouble(hj212Model.getVoltageA());
        double voltageB = Double.parseDouble(hj212Model.getVoltageB());
        double voltageC = Double.parseDouble(hj212Model.getVoltageC());
        double totalPower = Double.parseDouble(hj212Model.getTotalPower());
        String detectTime = hj212Model.getDetectTime();
        device device = new device();
        device.setMN(deviceMN);
        switch (deviceMN) {
            case "ZC20018192" -> {
                device.setDeviceName("精锻车间总1");
                if (!isOnline(detectTime)) {
                    device.setState(4);
                    device.setErrorMsg("设备不在线");
                    return device;
                } else {
                    if (!isNormalVoltage(voltageA, voltageB, voltageC)) {
                        device.setState(3);
                        device.setErrorMsg("电压正常范围为215-245，当前A电压：" + voltageA + " 当前B电压：" + voltageB + " 当前C电压：" + voltageC);
                        return device;
                    } else {
                        if (!isNormalTotalPower(10.26, 319.77, totalPower)) {
                            device.setState(2);
                            device.setErrorMsg("当前设备功率为：" + totalPower);
                            return device;
                        } else {
                            device.setState(1);
                            return device;
                        }
                    }
                }
            }
            case "ZC20018177" -> {
                device.setDeviceName("差速器总成生产设备");
                if (!isOnline(detectTime)) {
                    device.setState(4);
                    device.setErrorMsg("设备不在线");
                    return device;
                } else {
                    if (!isNormalVoltage(voltageA, voltageB, voltageC)) {
                        device.setState(3);
                        device.setErrorMsg("电压正常范围为215-245，当前A电压：" + voltageA + " 当前B电压：" + voltageB + " 当前C电压：" + voltageC);
                        return device;
                    } else {
                        if (!isNormalTotalPower(0.02, 103.09, totalPower)) {
                            device.setState(2);
                            device.setErrorMsg("当前设备功率为：" + totalPower);
                            return device;
                        } else {
                            device.setState(1);
                            return device;
                        }
                    }
                }
            }
            case "ZC20018175" -> {
                device.setDeviceName("热处理车间总");
                if (!isOnline(detectTime)) {
                    device.setState(4);
                    device.setErrorMsg("设备不在线");
                    return device;
                } else {
                    if (!isNormalVoltage(voltageA, voltageB, voltageC)) {
                        device.setState(3);
                        device.setErrorMsg("电压正常范围为215-245，当前A电压：" + voltageA + " 当前B电压：" + voltageB + " 当前C电压：" + voltageC);
                        return device;
                    } else {
                        if (!isNormalTotalPower(73.37, 640.30, totalPower)) {
                            device.setState(2);
                            device.setErrorMsg("当前设备功率为：" + totalPower);
                            return device;
                        } else {
                            device.setState(1);
                            return device;
                        }
                    }
                }
            }
            case "ZC20018164" -> {
                device.setDeviceName("1号抛丸机");
                if (!isOnline(detectTime)) {
                    device.setState(4);
                    device.setErrorMsg("设备不在线");
                    return device;
                } else {
                    if (!isNormalVoltage(voltageA, voltageB, voltageC)) {
                        device.setState(3);
                        device.setErrorMsg("电压正常范围为215-245，当前A电压：" + voltageA + " 当前B电压：" + voltageB + " 当前C电压：" + voltageC);
                        return device;
                    } else {
                        if (!isNormalTotalPower(0.01, 53.85, totalPower)) {
                            device.setState(2);
                            device.setErrorMsg("当前设备功率为：" + totalPower);
                            return device;
                        } else {
                            device.setState(1);
                            return device;
                        }
                    }
                }
            }
            case "ZC20018161" -> {
                device.setDeviceName("1号除尘器");
                if (!isOnline(detectTime)) {
                    device.setState(4);
                    device.setErrorMsg("设备不在线");
                    return device;
                } else {
                    if (!isNormalVoltage(voltageA, voltageB, voltageC)) {
                        device.setState(3);
                        device.setErrorMsg("电压正常范围为215-245，当前A电压：" + voltageA + " 当前B电压：" + voltageB + " 当前C电压：" + voltageC);
                        return device;
                    } else {
                        if (!isNormalTotalPower(0.03, 26.42, totalPower)) {
                            device.setState(2);
                            device.setErrorMsg("当前设备功率为：" + totalPower);
                            return device;
                        } else {
                            device.setState(1);
                            return device;
                        }
                    }
                }
            }
            case "ZC20018151" -> {
                device.setDeviceName("3号静电式油雾净化器");
                if (!isOnline(detectTime)) {
                    device.setState(4);
                    device.setErrorMsg("设备不在线");
                    return device;
                } else {
                    if (!isNormalVoltage(voltageA, voltageB, voltageC)) {
                        device.setState(3);
                        device.setErrorMsg("电压正常范围为215-245，当前A电压：" + voltageA + " 当前B电压：" + voltageB + " 当前C电压：" + voltageC);
                        return device;
                    } else {
                        if (!isNormalTotalPower(10.11, 14.38, totalPower)) {
                            device.setState(2);
                            device.setErrorMsg("当前设备功率为：" + totalPower);
                            return device;
                        } else {
                            device.setState(1);
                            return device;
                        }
                    }
                }
            }
            case "ZC20018124" -> {
                device.setDeviceName("热锻机800t-2");
                if (!isOnline(detectTime)) {
                    device.setState(4);
                    device.setErrorMsg("设备不在线");
                    return device;
                } else {
                    if (!isNormalVoltage(voltageA, voltageB, voltageC)) {
                        device.setState(3);
                        device.setErrorMsg("电压正常范围为215-245，当前A电压：" + voltageA + " 当前B电压：" + voltageB + " 当前C电压：" + voltageC);
                        return device;
                    } else {
                        // 一直未使用，一直为0
                        //if (!isNormalTotalPower(10, 20, totalPower)) {
                            device.setState(2);
                            device.setErrorMsg("当前设备功率为：" + totalPower);
                            return device;
//                        } else {
//                            device.setState(1);
//                            return device;
//                        }
                    }
                }
            }
            case "ZC20018123" -> {
                device.setDeviceName("二期机加工车间总");
                if (!isOnline(detectTime)) {
                    device.setState(4);
                    device.setErrorMsg("设备不在线");
                    return device;
                } else {
                    if (!isNormalVoltage(voltageA, voltageB, voltageC)) {
                        device.setState(3);
                        device.setErrorMsg("电压正常范围为215-245，当前A电压：" + voltageA + " 当前B电压：" + voltageB + " 当前C电压：" + voltageC);
                        return device;
                    } else {
                        if (!isNormalTotalPower(33.41, 532.75, totalPower)) {
                            device.setState(2);
                            device.setErrorMsg("当前设备功率为：" + totalPower);
                            return device;
                        } else {
                            device.setState(1);
                            return device;
                        }
                    }
                }
            }
            case "ZC20018121" -> {
                device.setDeviceName("热锻机1000t");
                if (!isOnline(detectTime)) {
                    device.setState(4);
                    device.setErrorMsg("设备不在线");
                    return device;
                } else {
                    if (!isNormalVoltage(voltageA, voltageB, voltageC)) {
                        device.setState(3);
                        device.setErrorMsg("电压正常范围为215-245，当前A电压：" + voltageA + " 当前B电压：" + voltageB + " 当前C电压：" + voltageC);
                        return device;
                    } else {
                        if (!isNormalTotalPower(0.11, 0.20, totalPower)) {
                            device.setState(2);
                            device.setErrorMsg("当前设备功率为：" + totalPower);
                            return device;
                        } else {
                            device.setState(1);
                            return device;
                        }
                    }
                }
            }
            case "ZC20018116" -> {
                device.setDeviceName("2号静电式油雾净化器");
                if (!isOnline(detectTime)) {
                    device.setState(4);
                    device.setErrorMsg("设备不在线");
                    return device;
                } else {
                    if (!isNormalVoltage(voltageA, voltageB, voltageC)) {
                        device.setState(3);
                        device.setErrorMsg("电压正常范围为215-245，当前A电压：" + voltageA + " 当前B电压：" + voltageB + " 当前C电压：" + voltageC);
                        return device;
                    } else {
                        if (!isNormalTotalPower(8.86, 11.55, totalPower)) {
                            device.setState(2);
                            device.setErrorMsg("当前设备功率为：" + totalPower);
                            return device;
                        } else {
                            device.setState(1);
                            return device;
                        }
                    }
                }
            }
            case "ZC20018073" -> {
                device.setDeviceName("一期机加工车间总2");
                if (!isOnline(detectTime)) {
                    device.setState(4);
                    device.setErrorMsg("设备不在线");
                    return device;
                } else {
                    if (!isNormalVoltage(voltageA, voltageB, voltageC)) {
                        device.setState(3);
                        device.setErrorMsg("电压正常范围为215-245，当前A电压：" + voltageA + " 当前B电压：" + voltageB + " 当前C电压：" + voltageC);
                        return device;
                    } else {
                        if (!isNormalTotalPower(15.03, 303.49, totalPower)) {
                            device.setState(2);
                            device.setErrorMsg("当前设备功率为：" + totalPower);
                            return device;
                        } else {
                            device.setState(1);
                            return device;
                        }
                    }
                }
            }
            case "ZC20018065" -> {
                device.setDeviceName("精锻车间总2");
                if (!isOnline(detectTime)) {
                    device.setState(4);
                    device.setErrorMsg("设备不在线");
                    return device;
                } else {
                    if (!isNormalVoltage(voltageA, voltageB, voltageC)) {
                        device.setState(3);
                        device.setErrorMsg("电压正常范围为215-245，当前A电压：" + voltageA + " 当前B电压：" + voltageB + " 当前C电压：" + voltageC);
                        return device;
                    } else {
                        if (!isNormalTotalPower(0.55, 23.58, totalPower)) {
                            device.setState(2);
                            device.setErrorMsg("当前设备功率为：" + totalPower);
                            return device;
                        } else {
                            device.setState(1);
                            return device;
                        }
                    }
                }
            }
            case "ZC20018060" -> {
                device.setDeviceName("7号除尘器");
                if (!isOnline(detectTime)) {
                    device.setState(4);
                    device.setErrorMsg("设备不在线");
                    return device;
                } else {
                    if (!isNormalVoltage(voltageA, voltageB, voltageC)) {
                        device.setState(3);
                        device.setErrorMsg("电压正常范围为215-245，当前A电压：" + voltageA + " 当前B电压：" + voltageB + " 当前C电压：" + voltageC);
                        return device;
                    } else {
                        if (!isNormalTotalPower(14.59, 19.80, totalPower)) {
                            device.setState(2);
                            device.setErrorMsg("当前设备功率为：" + totalPower);
                            return device;
                        } else {
                            device.setState(1);
                            return device;
                        }
                    }
                }
            }
            case "ZC20018054" -> {
                device.setDeviceName("4号静电式油雾净化器");
                if (!isOnline(detectTime)) {
                    device.setState(4);
                    device.setErrorMsg("设备不在线");
                    return device;
                } else {
                    if (!isNormalVoltage(voltageA, voltageB, voltageC)) {
                        device.setState(3);
                        device.setErrorMsg("电压正常范围为215-245，当前A电压：" + voltageA + " 当前B电压：" + voltageB + " 当前C电压：" + voltageC);
                        return device;
                    } else {
                        if (!isNormalTotalPower(0.04, 24.66, totalPower)) {
                            device.setState(2);
                            device.setErrorMsg("当前设备功率为：" + totalPower);
                            return device;
                        } else {
                            device.setState(1);
                            return device;
                        }
                    }
                }
            }
            case "ZC20018013" -> {
                device.setDeviceName("热锻机600t");
                if (!isOnline(detectTime)) {
                    device.setState(4);
                    device.setErrorMsg("设备不在线");
                    return device;
                } else {
                    if (!isNormalVoltage(voltageA, voltageB, voltageC)) {
                        device.setState(3);
                        device.setErrorMsg("电压正常范围为215-245，当前A电压：" + voltageA + " 当前B电压：" + voltageB + " 当前C电压：" + voltageC);
                        return device;
                    } else {
                        if (!isNormalTotalPower(1.65, 61.16, totalPower)) {
                            device.setState(2);
                            device.setErrorMsg("当前设备功率为：" + totalPower);
                            return device;
                        } else {
                            device.setState(1);
                            return device;
                        }
                    }
                }
            }
            case "ZC20018002" -> {
                device.setDeviceName("热处理多用炉");
                if (!isOnline(detectTime)) {
                    device.setState(4);
                    device.setErrorMsg("设备不在线");
                    return device;
                } else {
                    if (!isNormalVoltage(voltageA, voltageB, voltageC)) {
                        device.setState(3);
                        device.setErrorMsg("电压正常范围为215-245，当前A电压：" + voltageA + " 当前B电压：" + voltageB + " 当前C电压：" + voltageC);
                        return device;
                    } else {
                        if (!isNormalTotalPower(0.06, 2.70, totalPower)) {
                            device.setState(2);
                            device.setErrorMsg("当前设备功率为：" + totalPower);
                            return device;
                        } else {
                            device.setState(1);
                            return device;
                        }
                    }
                }
            }
            case "ZC20017973" -> {
                device.setDeviceName("模具加工中心");
                if (!isOnline(detectTime)) {
                    device.setState(4);
                    device.setErrorMsg("设备不在线");
                    return device;
                } else {
                    if (!isNormalVoltage(voltageA, voltageB, voltageC)) {
                        device.setState(3);
                        device.setErrorMsg("电压正常范围为215-245，当前A电压：" + voltageA + " 当前B电压：" + voltageB + " 当前C电压：" + voltageC);
                        return device;
                    } else {
                        if (!isNormalTotalPower(2.90, 52.45, totalPower)) {
                            device.setState(2);
                            device.setErrorMsg("当前设备功率为：" + totalPower);
                            return device;
                        } else {
                            device.setState(1);
                            return device;
                        }
                    }
                }
            }
            case "ZC20017897" -> {
                device.setDeviceName("3号除尘器");
                if (!isOnline(detectTime)) {
                    device.setState(4);
                    device.setErrorMsg("设备不在线");
                    return device;
                } else {
                    if (!isNormalVoltage(voltageA, voltageB, voltageC)) {
                        device.setState(3);
                        device.setErrorMsg("电压正常范围为215-245，当前A电压：" + voltageA + " 当前B电压：" + voltageB + " 当前C电压：" + voltageC);
                        return device;
                    } else {
                        if (!isNormalTotalPower(0.10, 41.88, totalPower)) {
                            device.setState(2);
                            device.setErrorMsg("当前设备功率为：" + totalPower);
                            return device;
                        } else {
                            device.setState(1);
                            return device;
                        }
                    }
                }
            }
            case "ZC20017935" -> {
                device.setDeviceName("2号除尘器");
                if (!isOnline(detectTime)) {
                    device.setState(4);
                    device.setErrorMsg("设备不在线");
                    return device;
                } else {
                    if (!isNormalVoltage(voltageA, voltageB, voltageC)) {
                        device.setState(3);
                        device.setErrorMsg("电压正常范围为215-245，当前A电压：" + voltageA + " 当前B电压：" + voltageB + " 当前C电压：" + voltageC);
                        return device;
                    } else {
                        if (!isNormalTotalPower(0.05, 20.39, totalPower)) {
                            device.setState(2);
                            device.setErrorMsg("当前设备功率为：" + totalPower);
                            return device;
                        } else {
                            device.setState(1);
                            return device;
                        }
                    }
                }
            }
            case "ZC20017933" -> {
                device.setDeviceName("精锻车间总4");
                if (!isOnline(detectTime)) {
                    device.setState(4);
                    device.setErrorMsg("设备不在线");
                    return device;
                } else {
                    if (!isNormalVoltage(voltageA, voltageB, voltageC)) {
                        device.setState(3);
                        device.setErrorMsg("电压正常范围为215-245，当前A电压：" + voltageA + " 当前B电压：" + voltageB + " 当前C电压：" + voltageC);
                        return device;
                    } else {
                        if (!isNormalTotalPower(0.90, 63.01, totalPower)) {
                            device.setState(2);
                            device.setErrorMsg("当前设备功率为：" + totalPower);
                            return device;
                        } else {
                            device.setState(1);
                            return device;
                        }
                    }
                }
            }
            case "ZC20017927" -> {
                device.setDeviceName("5号除尘器");
                if (!isOnline(detectTime)) {
                    device.setState(4);
                    device.setErrorMsg("设备不在线");
                    return device;
                } else {
                    if (!isNormalVoltage(voltageA, voltageB, voltageC)) {
                        device.setState(3);
                        device.setErrorMsg("电压正常范围为215-245，当前A电压：" + voltageA + " 当前B电压：" + voltageB + " 当前C电压：" + voltageC);
                        return device;
                    } else {
                        if (!isNormalTotalPower(34.20, 61.54, totalPower)) {
                            device.setState(2);
                            device.setErrorMsg("当前设备功率为：" + totalPower);
                            return device;
                        } else {
                            device.setState(1);
                            return device;
                        }
                    }
                }
            }
            case "ZC20017926" -> {
                device.setDeviceName("磷皂化线设备");
                if (!isOnline(detectTime)) {
                    device.setState(4);
                    device.setErrorMsg("设备不在线");
                    return device;
                } else {
                    if (!isNormalVoltage(voltageA, voltageB, voltageC)) {
                        device.setState(3);
                        device.setErrorMsg("电压正常范围为215-245，当前A电压：" + voltageA + " 当前B电压：" + voltageB + " 当前C电压：" + voltageC);
                        return device;
                    } else {
                        if (!isNormalTotalPower(0.36, 25.08, totalPower)) {
                            device.setState(2);
                            device.setErrorMsg("当前设备功率为：" + totalPower);
                            return device;
                        } else {
                            device.setState(1);
                            return device;
                        }
                    }
                }
            }
            case "ZC20017896" -> {
                device.setDeviceName("6号除尘器");
                if (!isOnline(detectTime)) {
                    device.setState(4);
                    device.setErrorMsg("设备不在线");
                    return device;
                } else {
                    if (!isNormalVoltage(voltageA, voltageB, voltageC)) {
                        device.setState(3);
                        device.setErrorMsg("电压正常范围为215-245，当前A电压：" + voltageA + " 当前B电压：" + voltageB + " 当前C电压：" + voltageC);
                        return device;
                    } else {
                        if (!isNormalTotalPower(0.04, 5.03, totalPower)) {
                            device.setState(2);
                            device.setErrorMsg("当前设备功率为：" + totalPower);
                            return device;
                        } else {
                            device.setState(1);
                            return device;
                        }
                    }
                }
            }
            case "ZC20017934" -> {
                device.setDeviceName("2号酸雾净化塔");
                if (!isOnline(detectTime)) {
                    device.setState(4);
                    device.setErrorMsg("设备不在线");
                    return device;
                } else {
                    if (!isNormalVoltage(voltageA, voltageB, voltageC)) {
                        device.setState(3);
                        device.setErrorMsg("电压正常范围为215-245，当前A电压：" + voltageA + " 当前B电压：" + voltageB + " 当前C电压：" + voltageC);
                        return device;
                    } else {
                        if (!isNormalTotalPower(21.17, 28.42, totalPower)) {
                            device.setState(2);
                            device.setErrorMsg("当前设备功率为：" + totalPower);
                            return device;
                        } else {
                            device.setState(1);
                            return device;
                        }
                    }
                }
            }
            case "ZC20017915" -> {
                device.setDeviceName("精锻车间总5");
                if (!isOnline(detectTime)) {
                    device.setState(4);
                    device.setErrorMsg("设备不在线");
                    return device;
                } else {
                    if (!isNormalVoltage(voltageA, voltageB, voltageC)) {
                        device.setState(3);
                        device.setErrorMsg("电压正常范围为215-245，当前A电压：" + voltageA + " 当前B电压：" + voltageB + " 当前C电压：" + voltageC);
                        return device;
                    } else {
                        if (!isNormalTotalPower(0.28, 0.36, totalPower)) {
                            device.setState(2);
                            device.setErrorMsg("当前设备功率为：" + totalPower);
                            return device;
                        } else {
                            device.setState(1);
                            return device;
                        }
                    }
                }
            }
            case "ZC20017905" -> {
                device.setDeviceName("1号酸雾净化塔");
                if (!isOnline(detectTime)) {
                    device.setState(4);
                    device.setErrorMsg("设备不在线");
                    return device;
                } else {
                    if (!isNormalVoltage(voltageA, voltageB, voltageC)) {
                        device.setState(3);
                        device.setErrorMsg("电压正常范围为215-245，当前A电压：" + voltageA + " 当前B电压：" + voltageB + " 当前C电压：" + voltageC);
                        return device;
                    } else {
                        if (!isNormalTotalPower(12.09, 16.31, totalPower)) {
                            device.setState(2);
                            device.setErrorMsg("当前设备功率为：" + totalPower);
                            return device;
                        } else {
                            device.setState(1);
                            return device;
                        }
                    }
                }
            }
            case "ZC20017937" -> {
                device.setDeviceName("精锻车间总3");
                if (!isOnline(detectTime)) {
                    device.setState(4);
                    device.setErrorMsg("设备不在线");
                    return device;
                } else {
                    if (!isNormalVoltage(voltageA, voltageB, voltageC)) {
                        device.setState(3);
                        device.setErrorMsg("电压正常范围为215-245，当前A电压：" + voltageA + " 当前B电压：" + voltageB + " 当前C电压：" + voltageC);
                        return device;
                    } else {
                        if (!isNormalTotalPower(0.90, 35.04, totalPower)) {
                            device.setState(2);
                            device.setErrorMsg("当前设备功率为：" + totalPower);
                            return device;
                        } else {
                            device.setState(1);
                            return device;
                        }
                    }
                }
            }
            case "ZC20017923" -> {
                device.setDeviceName("1号静电式油雾净化器");
                if (!isOnline(detectTime)) {
                    device.setState(4);
                    device.setErrorMsg("设备不在线");
                    return device;
                } else {
                    if (!isNormalVoltage(voltageA, voltageB, voltageC)) {
                        device.setState(3);
                        device.setErrorMsg("电压正常范围为215-245，当前A电压：" + voltageA + " 当前B电压：" + voltageB + " 当前C电压：" + voltageC);
                        return device;
                    } else {
                        if (!isNormalTotalPower(0.04, 26.35, totalPower)) {
                            device.setState(2);
                            device.setErrorMsg("当前设备功率为：" + totalPower);
                            return device;
                        } else {
                            device.setState(1);
                            return device;
                        }
                    }
                }
            }
            case "ZC20017936" -> {
                device.setDeviceName("2号抛丸机");
                if (!isOnline(detectTime)) {
                    device.setState(4);
                    device.setErrorMsg("设备不在线");
                    return device;
                } else {
                    if (!isNormalVoltage(voltageA, voltageB, voltageC)) {
                        device.setState(3);
                        device.setErrorMsg("电压正常范围为215-245，当前A电压：" + voltageA + " 当前B电压：" + voltageB + " 当前C电压：" + voltageC);
                        return device;
                    } else {
                        if (!isNormalTotalPower(0.01, 0.15, totalPower)) {
                            device.setState(2);
                            device.setErrorMsg("当前设备功率为：" + totalPower);
                            return device;
                        } else {
                            device.setState(1);
                            return device;
                        }
                    }
                }
            }
            case "ZC20017924" -> {
                device.setDeviceName("一期机加工车间总1");
                if (!isOnline(detectTime)) {
                    device.setState(4);
                    device.setErrorMsg("设备不在线");
                    return device;
                } else {
                    if (!isNormalVoltage(voltageA, voltageB, voltageC)) {
                        device.setState(3);
                        device.setErrorMsg("电压正常范围为215-245，当前A电压：" + voltageA + " 当前B电压：" + voltageB + " 当前C电压：" + voltageC);
                        return device;
                    } else {
                        if (!isNormalTotalPower(9.00, 97.57, totalPower)) {
                            device.setState(2);
                            device.setErrorMsg("当前设备功率为：" + totalPower);
                            return device;
                        } else {
                            device.setState(1);
                            return device;
                        }
                    }
                }
            }
            case "ZC20017931" -> {
                device.setDeviceName("热锻机800t-1");
                if (!isOnline(detectTime)) {
                    device.setState(4);
                    device.setErrorMsg("设备不在线");
                    return device;
                } else {
                    if (!isNormalVoltage(voltageA, voltageB, voltageC)) {
                        device.setState(3);
                        device.setErrorMsg("电压正常范围为215-245，当前A电压：" + voltageA + " 当前B电压：" + voltageB + " 当前C电压：" + voltageC);
                        return device;
                    } else {
                        if (!isNormalTotalPower(0.02, 82.57, totalPower)) {
                            device.setState(2);
                            device.setErrorMsg("当前设备功率为：" + totalPower);
                            return device;
                        } else {
                            device.setState(1);
                            return device;
                        }
                    }
                }
            }
            case "ZC20017939" -> {
                device.setDeviceName("4号除尘器");
                if (!isOnline(detectTime)) {
                    device.setState(4);
                    device.setErrorMsg("设备不在线");
                    return device;
                } else {
                    if (!isNormalVoltage(voltageA, voltageB, voltageC)) {
                        device.setState(3);
                        device.setErrorMsg("电压正常范围为215-245，当前A电压：" + voltageA + " 当前B电压：" + voltageB + " 当前C电压：" + voltageC);
                        return device;
                    } else {
                        if (!isNormalTotalPower(2.47, 9.27, totalPower)) {
                            device.setState(2);
                            device.setErrorMsg("当前设备功率为：" + totalPower);
                            return device;
                        } else {
                            device.setState(1);
                            return device;
                        }
                    }
                }
            }
            case "ZC20017925" -> {
                device.setDeviceName("3号抛丸机");
                if (!isOnline(detectTime)) {
                    device.setState(4);
                    device.setErrorMsg("设备不在线");
                    return device;
                } else {
                    if (!isNormalVoltage(voltageA, voltageB, voltageC)) {
                        device.setState(3);
                        device.setErrorMsg("电压正常范围为215-245，当前A电压：" + voltageA + " 当前B电压：" + voltageB + " 当前C电压：" + voltageC);
                        return device;
                    } else {
                        if (!isNormalTotalPower(0.90, 72.90, totalPower)) {
                            device.setState(2);
                            device.setErrorMsg("当前设备功率为：" + totalPower);
                            return device;
                        } else {
                            device.setState(1);
                            return device;
                        }
                    }
                }
            }
            case "ZC20015945" -> {
                device.setDeviceName("抗磨磷化线设备");
                if (!isOnline(detectTime)) {
                    device.setState(4);
                    device.setErrorMsg("设备不在线");
                    return device;
                } else {
                    if (!isNormalVoltage(voltageA, voltageB, voltageC)) {
                        device.setState(3);
                        device.setErrorMsg("电压正常范围为215-245，当前A电压：" + voltageA + " 当前B电压：" + voltageB + " 当前C电压：" + voltageC);
                        return device;
                    } else {
                        if (!isNormalTotalPower(0.45, 291.00, totalPower)) {
                            device.setState(2);
                            device.setErrorMsg("当前设备功率为：" + totalPower);
                            return device;
                        } else {
                            device.setState(1);
                            return device;
                        }
                    }
                }
            }
            default -> {
                device.setState(4);
                device.setErrorMsg("设备不在线");
                device.setDeviceName("测试数据");
                return device;
            }
        }
    }

    public Boolean isOnline(String detectTime) {
        DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
        LocalDateTime parsedDetectTime = LocalDateTime.parse(detectTime, formatter);
        LocalDateTime currentDateTime = LocalDateTime.now();
        Duration duration = Duration.between(parsedDetectTime, currentDateTime);
        if(duration.toMinutes() > 45) {
            return false;
        }
        return true;
    }

    // 三相电压是否是正常工作电压
    public Boolean isNormalVoltage(double voltageA, double voltageB, double voltageC) {
        return (voltageA >= 215 && voltageA <= 245) && (voltageB >= 215 && voltageB <= 245) && (voltageC >= 215 && voltageC <= 245);
    }

    // 总功率是否正常
    public Boolean isNormalTotalPower(double lowTotalPower, double highTotalPower, double totalPower) {
        return  totalPower >= lowTotalPower && totalPower <= highTotalPower;
    }
}
